//
//  ViewController.m
//  GSTabbarDemo
//
//  Created by 陈JJ on 2018/11/21.
//  Copyright © 2018年 陈JJ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *popBtn;
@property (nonatomic, strong) UIButton *rootBtn;

@end

@implementation ViewController

#pragma mark - system callback

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self commonInit];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.title = [NSString stringWithFormat:@"page_%ld",self.navigationController.viewControllers.count-1];
    self.popBtn.hidden = self.navigationController.viewControllers.count <= 1;
    self.rootBtn.hidden = self.navigationController.viewControllers.count <= 2;
}

#pragma mark - pravite method

- (void)commonInit {
    
    CGRect vFrame = self.view.bounds;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"PUSH" forState:UIControlStateNormal];
    btn.frame = CGRectMake(vFrame.size.width * 0.5 - 40, vFrame.size.height * 0.5 - 200, 80, 50);
    btn.backgroundColor = [UIColor grayColor];
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *popBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [popBtn setTitle:@"POP" forState:UIControlStateNormal];
    popBtn.frame = CGRectMake(vFrame.size.width * 0.5 - 40, vFrame.size.height * 0.5 - 100, 80, 50);
    popBtn.backgroundColor = [UIColor grayColor];
    [popBtn addTarget:self action:@selector(popBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:popBtn];
    _popBtn = popBtn;
    
    UIButton *rootBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rootBtn setTitle:@"ROOT" forState:UIControlStateNormal];
    rootBtn.frame = CGRectMake(vFrame.size.width * 0.5 - 40, vFrame.size.height * 0.5 , 80, 50);
    rootBtn.backgroundColor = [UIColor grayColor];
    [rootBtn addTarget:self action:@selector(rootBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rootBtn];
    _rootBtn = rootBtn;
    
}

- (void)btnAction {
    ViewController *vc = [[ViewController alloc] init];
    vc.view.backgroundColor = [self randomColor];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)popBtnAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rootBtnAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (UIColor *)randomColor {
    return [UIColor colorWithRed:(arc4random()%256)/256.f
                           green:(arc4random()%256)/256.f
                            blue:(arc4random()%256)/256.f
                           alpha:1.0f];
}

@end
